package com.gzsxy.esjy.service.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzsxy.esjy.bus.log.annotation.OperLog;
import com.gzsxy.esjy.bus.log.enum1.LogActionType;
import com.gzsxy.esjy.service.base.dto.request.ProductGroups;
import com.gzsxy.esjy.service.base.dto.request.ProductRequest;
import com.gzsxy.esjy.service.base.dto.response.DefaultResponse;

import com.gzsxy.esjy.service.base.dto.response.PageResult;
import com.gzsxy.esjy.service.base.dto.response.ProductRsp;
import com.gzsxy.esjy.service.product.entity.Product;
import com.gzsxy.esjy.service.product.entity.vo.ProductQueryVo;
import com.gzsxy.esjy.service.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 商品管理
 * @date 2022/1/10 22:18
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/admin/product")
@Api("商品管理")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * @description: 分页查询商品
     * @param page 页数
     * @param limit 记录数
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("分页查询")
    public DefaultResponse list(@ApiParam(value = "当前页面",required = false,defaultValue ="1" ) @RequestParam(required = false) Long page,         //long数据类型适用于较大整数类型
                                @ApiParam(value = "每页记录数",required = false,defaultValue ="5") @RequestParam(required = false) Long limit,
                                @ApiParam("查询对象") ProductQueryVo productQueryVo){
        IPage<Product> pageParam = null;
        //分页查询
        if (page != null && limit != null){
            pageParam = new Page<>(page, limit);
        }
        PageResult<ProductRsp> productRsp = productService.selectPage(pageParam,productQueryVo);
        return DefaultResponse.ok(productRsp);
    }

    @PostMapping("/add")
    @ApiOperation("添加商品")
    @OperLog(operModul = "商品管理",operType = LogActionType.ADD,operDesc = "添加商品")
    public DefaultResponse addProduct(@Validated @RequestBody ProductRequest productRequest){
        productService.addProduct(productRequest);
        return DefaultResponse.ok();
    }

    @PutMapping("/update")
    @ApiOperation("更新商品")
    @OperLog(operModul = "商品管理",operType = LogActionType.UPDATE,operDesc = "更新商品")
    public DefaultResponse updateProduct(@Validated({ProductGroups.class}) @RequestBody ProductRequest productRequest){
        productService.updateProduct(productRequest);
        return DefaultResponse.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除商品")
    @OperLog(operModul = "商品管理",operType = LogActionType.DELETE,operDesc = "删除商品")
    public DefaultResponse deleteProduct(@ApiParam(value = "分类id",required = true) @PathVariable String id){
        productService.deleteProduct(id);
        return DefaultResponse.ok();
    }


    @GetMapping ("/edit/{id}")
    @ApiOperation("根据商品id修改发布状态")
    @OperLog(operModul = "商品管理",operType = LogActionType.UPDATE,operDesc = "根据商品id修改发布状态")
    public DefaultResponse publishById(@ApiParam(value = "商品id",required = true) @PathVariable String id,
                                       @RequestParam Integer publishStatus){
        productService.publishById(id,publishStatus);
        return DefaultResponse.ok();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据商品id查询商品(外部服务调用) ")
    public DefaultResponse getById(@PathVariable String id){
        ProductRsp product = productService.getProById(id);
        return DefaultResponse.ok(product);
    }

    @PostMapping("/editNumber/{id}")
    @ApiOperation("更新商品库存（外部服务调用） ")
    public DefaultResponse editNumber(@PathVariable String id,@RequestBody Integer number){
        productService.editNumber(id,number);
        return DefaultResponse.ok();
    }

}
